<template>
	<div>
		<div style="padding: 10px 0;text-align: right;">
			<el-button @click="addCate" type="primary">添加分类</el-button>
		</div>
		<el-table
			:data="tableData"
			stripe
			style="width: 100%">

			<el-table-column
			  align="center"
			  prop="name"
			  label="分类名称"
			  >
			</el-table-column>
			<el-table-column
			  prop="remark"
			  label="简介"
			  align="center"
			  >
			</el-table-column>
			<el-table-column
			  label="操作"
			  align="center"
			  >
			  <template slot-scope="scope">
				<el-button type="text" @click="delCate(scope.row)" size="small">删除</el-button>
				<el-button type="text" @click="editCate(scope.row)" size="small">编辑</el-button>
			  </template>
			</el-table-column>
		</el-table>
		<el-dialog :title="this.form.id?'编辑分类':'添加分类'" :visible.sync="dialogFormVisible" >
		  <el-form  :label-width="'120px'">
			<el-form-item label="分类名称" >
			  <el-input v-model="form.name" ></el-input>
			  </el-form-item>
			<el-form-item label="简介" >
			  <el-input v-model="form.remark" placeholder="分类简介" ></el-input>
			  </el-form-item>
		  </el-form>
		  <div slot="footer" class="dialog-footer">
			<el-button @click="dialogFormVisible = false">取 消</el-button>
			<el-button type="primary" @click="createCate">确 定</el-button>
		  </div>
		</el-dialog>
	</div>
</template>

<script>
    import ajax from '@/ajax';
	 export default {
	   name: 'vegetables',
	   data: function(){
				return {
					dialogFormVisible:false,
					form:{
                        id:"",
						name:"",
						remark:"",
                        type:"goods_type"
					},
					tableData:[

					]
				}
		},
        mounted(){
            this.getList()
        },
        methods:{
            getList(){
                ajax.get("/tianhuiMarket/v1/enum/query",{params:{
                type: "goods_type"
                }}).then(res=>{
                    this.tableData = res.result;
                })
            },
            addCate(){
                this.dialogFormVisible = true;
                this.form.name = "";
                this.form.remark = "";
                this.form.id = "";
            },
            editCate(row){
                this.dialogFormVisible = true;
                this.form.name = row.name;
                this.form.remark = row.remark || "";
                this.form.id = row.id;
            },
            createCate(){
                if(!this.form.name){
                    this.$message.error("分类名称为必填！");
                    return
                }
                if(this.form.id){
                    ajax.put("/tianhuiMarket/v1/enum/update",this.form).then(res=>{
                        this.$message.success("操作成功！");
                        this.dialogFormVisible = false;
                        this.getList();
                    })
                }else{
                    ajax.post("/tianhuiMarket/v1/enum/create",this.form).then(res=>{
                        this.$message.success("操作成功！");
                        this.dialogFormVisible = false;
                        this.getList();
                    })
                }

            },
            delCate(row){
                ajax.delete("/tianhuiMarket/v1/enum/delete",{params:{id:row.id}}).then(res=>{
                    this.$message.success("操作成功！");
                    this.getList();
                })
            },
            updateCate(){
                if(!this.form.name){
                    this.$message.error("分类名称为必填！");
                    return
                }
                ajax.put("/tianhuiMarket/v1/enum/update",this.form).then(res=>{
                    this.$message.success("操作成功！");
                    this.dialogFormVisible = false;
                    this.getList();
                })
            },
        }
		}

</script>

<style>
</style>
